package com.estore.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.estore.entity.Categories;
import com.estore.entity.MainCategories;
import com.estore.info.AgentProductInfo;
//import com.estore.info.CatInfo;
import com.estore.info.ProductInfo;
import com.estore.info.ProfileInfo;
import com.estore.info.SubCatInfo;
import com.estore.manager.ProductManager;
import com.estore.manager.ProfileManager;
import com.estore.manager.ShopManager;
import com.estore.model.ProductModel;
import com.estore.model.ProfileModel;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class StartupAction extends ActionSupport implements
		ModelDriven<ProductModel> {
	private static final long serialVersionUID = 6535875941579248647L;
	private ProductManager productManager;
	private ProfileManager profileManager;
	private ShopManager shopManager;
	private List<SubCatInfo> listSubCat;
	private List<ProductInfo> list12NewProduct;
	private List<ProductInfo> list12MostFavoriteProduct;
	private ProductModel productModel = new ProductModel();
	private ProfileModel profileModel = new ProfileModel();
	
	private String term;

	public String startup() {
		Map<String, Object> session = ActionContext.getContext().getSession();
		if ( session.get("isBanned") == null || session.get("isBanned").equals(0) ) {
			List<ProductInfo> listProduct = productManager.getTop12NewProduct();
			List<ProductInfo> listProductResult = new ArrayList<ProductInfo>();
			for(ProductInfo record : listProduct){
				AgentProductInfo agentProduct = shopManager.getLowestPrice(Integer.parseInt(record.getProductID()));
				if(agentProduct != null){
					record.setSellingPrice(agentProduct.getSellingPrice());
					if(agentProduct.getDiscount() != null){
						record.setDiscount(agentProduct.getDiscount());
						record.setDiscountPercent(agentProduct.getDiscountPercent());
					}
				}
				listProductResult.add(record);
			}
			List<ProductInfo> list12FavoriteProduct = productManager.list12MostFavoriteProduct();
			List<ProductInfo> list12FavoriteProductResult = new ArrayList<ProductInfo>();
			for(ProductInfo record : list12FavoriteProduct){
				AgentProductInfo agentProduct = shopManager.getLowestPrice(Integer.parseInt(record.getProductID()));
				if(agentProduct != null){
					record.setSellingPrice(agentProduct.getSellingPrice());
					if(agentProduct.getDiscount() != null){
						record.setDiscount(agentProduct.getDiscount());
						record.setDiscountPercent(agentProduct.getDiscountPercent());
					}
				}
				list12FavoriteProductResult.add(record);
			}
			listSubCat = productManager.listSubCatInfo();
			setList12NewProduct(listProductResult);
			setList12MostFavoriteProduct(list12FavoriteProductResult);
			productModel.setAutoComplete(productManager.getAutoCompleteList());
			return "success";
		} else {
			return "banned";
		}
	}

	// list all profile
	public String adminstartup() {
		Map<String, Object> session = ActionContext.getContext().getSession();
		if (session.get("isBanned") == null || session.get("isBanned").equals(0)) {

			if (session.get("role") == null) {
				return "login";
			} else if (session.get("role").equals(1)
					|| session.get("role").equals(3)) {
				List<ProfileInfo> listprofileinfo = profileManager
						.getListProfileInfo();
				if (listprofileinfo != null) {
					profileModel.setListprofileinfo(listprofileinfo);
				}
				return "success";
			} else if (session.get("role").equals(2)) {
				productModel.setListSubcat(productManager.listSubCatInfo());
				productModel.setListCategories(productManager.listCategories());
				return "product";
			} else {
				return ERROR;
			}
		} else {
			return "banned";
		}
	}

	public List<SubCatInfo> getListsubcat() {
		return listSubCat;
	}

	public void setListsubcat(List<SubCatInfo> listsubcat) {
		this.listSubCat = listsubcat;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	public ProductManager getProductManager() {
		return productManager;
	}

	public void setProductManager(ProductManager productManager) {
		this.productManager = productManager;
	}

	public ProfileManager getProfileManager() {
		return profileManager;
	}

	public void setProfileManager(ProfileManager profileManager) {
		this.profileManager = profileManager;
	}

	public ProductModel getProductModel() {
		return productModel;
	}

	public void setProductModel(ProductModel productModel) {
		this.productModel = productModel;
	}

	public ProfileModel getProfileModel() {
		return profileModel;
	}

	public void setProfileModel(ProfileModel profileModel) {
		this.profileModel = profileModel;
	}

	@Override
	public ProductModel getModel() {
		return null;
	}

	public List<ProductInfo> getList12NewProduct() {
		return list12NewProduct;
	}

	public void setList12NewProduct(List<ProductInfo> list12NewProduct) {
		this.list12NewProduct = list12NewProduct;
	}

	public List<SubCatInfo> getListSubCat() {
		return listSubCat;
	}

	public void setListSubCat(List<SubCatInfo> listSubCat) {
		this.listSubCat = listSubCat;
	}

	public ShopManager getShopManager() {
		return shopManager;
	}

	public void setShopManager(ShopManager shopManager) {
		this.shopManager = shopManager;
	}

	public String getTerm() {
		return term;
	}

	public void setTerm(String term) {
		this.term = term;
	}

	public List<ProductInfo> getList12MostFavoriteProduct() {
		return list12MostFavoriteProduct;
	}

	public void setList12MostFavoriteProduct(
			List<ProductInfo> list12MostFavoriteProduct) {
		this.list12MostFavoriteProduct = list12MostFavoriteProduct;
	}

}
